'use strict';
const dayjs = require("dayjs");
module.exports = app => {
    const { STRING, INTEGER, TINYINT } = app.Sequelize;
    const Model = app.model.define(
        'deviceFaults',
        {
            id: {type: INTEGER, primaryKey: true, autoIncrement: true},
            /**
             *  `farmview` int NOT NULL COMMENT '房间',
             *   `device` int NOT NULL COMMENT '设备',
             *   `fault_time` date DEFAULT NULL COMMENT '故障时间',
             *   `complete_time` date DEFAULT NULL COMMENT '解决时间',
             *   `responser` varchar(20) DEFAULT NULL COMMENT '负责人',
             *   `fault_desc` varchar(200) CHARACTER SET utf8mb4 COLLATE utf8mb4_0900_ai_ci DEFAULT NULL COMMENT '备注',
             *   `complete_desc` varchar(255) DEFAULT NULL,
             *   `status` tinyint(1) DEFAULT NULL COMMENT '0 待处理 1处理中 2已完成 -1处理失败',
             *   `version` int DEFAULT '1',
             *   `is_deleted` tinyint(1) DEFAULT '0',
             *
             */
            farmview: { type: INTEGER, allowNull: true, comment: '房间' },
            device: { type: INTEGER, allowNull: true, comment: '设备' },
            responser: { type: STRING(20), allowNull: true, comment: '负责人' },
            fault_desc: { type: STRING(200), allowNull: true, comment: '备注' },
            complete_desc: { type: STRING(255), allowNull: true },
            status: { type: TINYINT, allowNull: true, comment: '0 待处理 1处理中 2已完成 -1处理失败' },
            version: { type: INTEGER, allowNull: true, defaultValue: 1 },
            is_deleted: { type: TINYINT, allowNull: true, defaultValue: 0 },
            fault_time:{
                type: app.Sequelize.DATE,
                allowNull: true,
                get() {
                    return dayjs(this.getDataValue('fault_time')).format('YYYY-MM-DD');
                }
            },
            complete_time:{
                type: app.Sequelize.DATE,
                allowNull: true,
                get() {
                    return dayjs(this.getDataValue('complete_time')).format('YYYY-MM-DD');
                }
            }
        },
        {
            tableName: 'device_faults',
            timestamps: true, // 启用时间戳(createdAt, updatedAt)
            updatedAt: false, // 不想要 updatedAt
            createdAt: false, // 想要  createdAt 但是希望名称叫做 created_at
        }
    );
    Model.associate = function() {
        app.model.DeviceFaults.belongsTo(app.model.Room, { foreignKey: 'farmview', targetKey: 'id' });
        // app.model.DeviceFaults.belongsTo(app.model.Device, { foreignKey: 'device', targetKey: 'id' });
    }
    return Model
};
